package org.btik.lightweb.client;

import org.btik.light.meta.base.video.VideoChannelType;
import org.btik.light.server.platform.common.api.service.OperationServer;
import org.btik.light.meta.DevMo;
import org.btik.light.meta.LightNode;
import org.btik.light.server.platform.common.api.bean.dev.manage.BindVideoChannelVo;
import org.btik.light.server.platform.common.api.bean.dev.manage.DevProp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;


@RestController
@RequestMapping("/dm")
public class DevManage {
    @Autowired
    private OperationServer manageServer;


    @RequestMapping("/list")
    public Collection<DevMo> list(String node) {
        if (ObjectUtils.isEmpty(node)) {
            node = LightNode.ROOT;
        }
        return manageServer.listAtNode(node);
    }
    @GetMapping("/getDevMo")
    public DevMo getDevMo(String sn) {
        return manageServer.getDevMo(sn);
    }

    @PostMapping("/attachVideoChannel")
    public boolean attachVideoChannel(@RequestBody BindVideoChannelVo bindVideoChannelVo) {
        int channelType = bindVideoChannelVo.channelType();
        try {
            VideoChannelType videoChannelType = VideoChannelType.valueOf(channelType);
            return manageServer.attachVideoChannel(videoChannelType, bindVideoChannelVo.channelIndex(), bindVideoChannelVo.devSn());
        } catch (IndexOutOfBoundsException e) {
            return false;
        }

    }
    @PostMapping("/updateProp")
    public boolean updateProp(@RequestBody DevProp devProp) {
        try {
            return manageServer.updateProp(devProp);
        } catch (Exception e) {
            return false;
        }
    }


}
